#include "mswin/module.h"
#include <sstream>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "throwWin32Error.h"

namespace mswin {

std::string exe_mod_date()
{
	std::string path;
	path.resize(MAX_PATH);
	path.resize(GetModuleFileName(0, &*path.begin(), MAX_PATH));
	if (path.empty())
		throwWin32Error("GetModuleFileName");

	HANDLE hFile = CreateFile(path.c_str(), 0/*GENERIC_WRITE*/, 0, 0, 
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

	if (hFile == INVALID_HANDLE_VALUE)
		throwWin32Error("CreateFile");

	FILETIME LastWriteTime;

	if (!GetFileTime(hFile, 0, 0, &LastWriteTime))
		throwWin32Error("GetFileTime");

	SYSTEMTIME SysTime;
	if (!FileTimeToSystemTime(&LastWriteTime, &SysTime))
		throwWin32Error("FileTimeToSystemTime");

	std::ostringstream date;
	date << SysTime.wYear << '.';
	date.fill('0');
	date.width(2);
	date << SysTime.wMonth << '.';
	date.width(2);
	date << SysTime.wDay;

	return date.str();
}

} //namespace mswin
